package com.atguigu.admin;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.Duration;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.*;


@SpringBootTest
public class AssertionsDemo {

    @Test
    void simpleAssertion(){
        assertEquals(2,1+1);

        assertNotEquals(3, 1 + 1);

        assertNotSame(new Object(), new Object());
        Object obj = new Object();
        assertSame(obj, obj);

        assertFalse(1 > 2);
        assertTrue(1 < 2);

        assertNull(null);
        assertNotNull(new Object());
    }

    /**
     * 数组断言
     */
    @Test
    @DisplayName("array assertion")
    public void array() {
        assertArrayEquals(new int[]{1, 2}, new int[] {1, 2});
    }

    /**
     * 组合断言
     */
    @Test
    @DisplayName("assert all")
    public void all() {
        assertAll("Math",
                () -> assertEquals(2, 1 + 1),
                () -> assertTrue(1 > 0)
        );
    }

    /**
     * 异常断言
     */
    @Test
    @DisplayName("异常测试")
    public void exceptionTest() {
        ArithmeticException exception = Assertions.assertThrows(
                //扔出断言异常
                ArithmeticException.class, () -> System.out.println(1 % 0));

    }

    @Test
    @DisplayName("超时测试")
    public void timeoutTest() {
        //如果测试方法时间超过1s将会异常
        Assertions.assertTimeout(Duration.ofMillis(1000), () -> Thread.sleep(500));
    }

    @Test
    @DisplayName("fail")
    public void shouldFail() {
        fail("This should fail");
    }

//    @DisplayName("前置条件")
//    public class AssumptionsTest {
//        private final String environment = "DEV";

//        @Test
//        @DisplayName("simple")
//        public void simpleAssume() {
//            assumeTrue(Objects.equals(this.environment, "DEV"));
//            assumeFalse(() -> Objects.equals(this.environment, "PROD"));
//        }
//
//        @Test
//        @DisplayName("assume then do")
//        public void assumeThenDo() {
//            assumingThat(
//                    Objects.equals(this.environment, "DEV"),
//                    () -> System.out.println("In DEV")
//            );
//        }
//    }
    @Test
    void test(){
        System.out.println("testAssertion");

    }


}
